package com.servlet;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

/**
 * Servlet implementation class ChooseFlightServlet
 */
public class ChooseFlightServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
private final static String DATASOURCE_NAME = "jdbc/FlyGreenwichAirline";
	
	private DataSource datasource;
    
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ChooseFlightServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name = request.getParameter("name");
		String customerid = request.getParameter("customer_id");
		String flightid = request.getParameter("flight_id");
		String date = request.getParameter("date");
		String seat = request.getParameter("decide");
		
		String depart = "";
		String dest = "";
		String cost = "";
		
		Timestamp timestamp = null;
		Integer flight_id = Integer.parseInt(flightid);
		Integer customer_id = Integer.parseInt(customerid);
		Integer seat_no = Integer.parseInt(seat);
		
		int bookingno = new Double( Math.random() * 100000 ).intValue();

		
		SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
		try {
			Date parsedDate = dateFormat.parse(date);
			timestamp = new Timestamp(parsedDate.getTime());
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		
		java.sql.Connection con=null;
		
		System.out.println("customerid: " + customerid);
		System.out.println("flightid: " + flightid);
		System.out.println("date: " + date);
		System.out.println("seat: " + seat);
		System.out.println("bookingno: " + bookingno);
		
    	String query = "INSERT INTO transactions (flight_id,customer_id,seat_no,select_time,booking_no) values (?,?,?,?,?);";
    	String query2 = "SELECT depart, dest, cost FROM flight WHERE flight_no="+flight_id+";";
    	
    	try{
    		Context env = (Context) new InitialContext().lookup("java:comp/env");
			datasource = (DataSource) env.lookup(DATASOURCE_NAME);
    		con = datasource.getConnection();
    	
    		java.sql.PreparedStatement pst = con.prepareStatement(query);
    		
    		pst.setInt(1, flight_id);
    		pst.setInt(2, customer_id);
    		pst.setInt(3, seat_no);
    		pst.setTimestamp(4, timestamp);
    		pst.setInt(5, bookingno);
    		
    		pst.executeUpdate();
    		
    		java.sql.Statement stmt2 = con.createStatement();
            ResultSet rs = stmt2.executeQuery (query2);
            
            while(rs.next()){
            	depart = rs.getString(1);
            	dest = rs.getString(2);
            	cost = rs.getString(3);
            }
    		
            rs.close();
            stmt2.close();
            
    	}catch(SQLException e){
    		e.printStackTrace();
    	}catch(Exception e){
    		e.printStackTrace();
    	}finally{
    		try{
    			if (con != null) con.close();
    		}catch(SQLException ignored){
    		ignored.printStackTrace();
    		}
    	}
    	
    	request.setAttribute("name", name);
    	request.setAttribute("date", date);
    	request.setAttribute("seat", seat);
    	request.setAttribute("depart", depart);
    	request.setAttribute("dest", dest);
    	request.setAttribute("cost", cost);
    	request.setAttribute("bookingno", bookingno);
    	
    	String nextJSP = "/successBook.jsp";
    	RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
    	dispatcher.forward(request,response);
	}

}
